package redis.gambing;

import org.apache.storm.redis.common.mapper.RedisDataTypeDescription;
import org.apache.storm.redis.common.mapper.RedisLookupMapper;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.ITuple;
import org.apache.storm.tuple.Values;

import java.util.ArrayList;
import java.util.List;

import static redis.gambing.GambleFinalString.*;

/**
 * 获取赌徒的总资产，并与上游信息合并后发送到下游
 */
public class GamblerAssertsInfoLookupMapper implements RedisLookupMapper {
    private final RedisDataTypeDescription description;

    public GamblerAssertsInfoLookupMapper() {
        description = new RedisDataTypeDescription(RedisDataTypeDescription.RedisDataType.HASH, HASH_GAMBLER_3_ASSERTS);
    }

    @Override
    public List<Values> toTuple(ITuple input, Object value) {
        String gamblerId = getKeyFromTuple(input);
        Long money = input.getLongByField(HASH_GAMBLER_1_MONEY);
        Short times = input.getShortByField(HASH_GAMBLER_2_TIMES);
        List<Values> values = new ArrayList<>();
        values.add(new Values(gamblerId, money, times, Long.parseLong(String.valueOf(value))));
        return values;
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        declarer.declare(new Fields(GAMBLER_ID, HASH_GAMBLER_1_MONEY, HASH_GAMBLER_2_TIMES, HASH_GAMBLER_3_ASSERTS));
    }

    @Override
    public RedisDataTypeDescription getDataTypeDescription() {
        return description;
    }

    @Override
    public String getKeyFromTuple(ITuple tuple) {
        return tuple.getStringByField(GAMBLER_ID);
    }

    @Override
    public String getValueFromTuple(ITuple tuple) {
        return null;
    }
}
